package com.example.demo.controller;

import com.example.demo.entity.BookResource;
import com.example.demo.mapper.BookResourceMapper;
import com.example.demo.utils.PagingResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/book-resource") // 控制器的前缀地址
public class BookResourceController {
    @Autowired
    BookResourceMapper bookResourceMapper;

    /**
     * 获取分页的图书资源数据
     * @param limit 每页数量
     * @param offset 偏移量
     * @param field 搜索字段
     * @param code 搜索关键词
     * @return 分页结果
     */
    @GetMapping("/list")
    public PagingResult<BookResource> select(
            @RequestParam(required = false, name = "limit", defaultValue = "10") int limit,
            @RequestParam(required = false, name = "offset", defaultValue = "0") int offset,
            @RequestParam(required = false, name = "field") String field,
            @RequestParam(required = false, name = "code") String code
    ) {
        List<BookResource> bookResourceList;
        int total;
        
        if (field != null && code != null && !code.trim().isEmpty()) {
            bookResourceList = bookResourceMapper.findByPageAndSearch(offset, limit, field, code);
            total = bookResourceMapper.countBySearch(field, code);
        } else {
            bookResourceList = bookResourceMapper.findByPage(offset, limit);
            total = bookResourceMapper.count();
        }
        
        return new PagingResult<BookResource>(bookResourceList, total);
    }

    /**
     * 根据ID获取图书资源
     * @param id 资源ID
     * @return 图书资源对象
     */
    @GetMapping("/{id}")
    public BookResource getById(@PathVariable("id") int id) {
        return bookResourceMapper.findById(id);
    }

    /**
     * 根据图书ID获取图书资源列表
     * @param bookId 图书ID
     * @return 图书资源列表
     */
    @GetMapping("/book/{bookId}")
    public List<BookResource> getByBookId(@PathVariable("bookId") int bookId) {
        return bookResourceMapper.findByBookId(bookId);
    }

    /**
     * 根据资源类型获取图书资源列表
     * @param type 资源类型
     * @return 图书资源列表
     */
    @GetMapping("/type/{type}")
    public List<BookResource> getByType(@PathVariable("type") String type) {
        return bookResourceMapper.findByType(type);
    }

    /**
     * 添加图书资源
     * @param bookResource 图书资源对象
     * @return 影响行数
     */
    @PostMapping("/add")
    public int add(@RequestBody BookResource bookResource) {
        return bookResourceMapper.insert(bookResource);
    }

    /**
     * 更新图书资源
     * @param bookResource 图书资源对象
     * @return 影响行数
     */
    @PutMapping("/update")
    public int update(@RequestBody BookResource bookResource) {
        return bookResourceMapper.update(bookResource);
    }

    /**
     * 删除图书资源
     * @param id 资源ID
     * @return 影响行数
     */
    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable("id") int id) {
        return bookResourceMapper.delete(id);
    }
}